<?php
class ReflectorFormat
{
	function reflect($rawDoc)
	{
		jimport('joomla.html.pane');

		$document = JFactory::getDocument();
		$document->addStyleSheet('components/com_jclassdocumentor/assets/css/accordion.css');

		$pane =& JPane::getInstance('sliders');

		$wikiClassPrefix = 'Joomla!_Programmierung/Framework';
		$wikiClassPage = '';
		$wikiMethodsPages = array();

		$wikiMethodsPages = array();

		$html = '';
		$html .=  $pane->startPane("the-pane");

		foreach ($rawDoc->classes as $class)
		{
			$wikiClassPage .= '{{RightTOC}}';
			$wikiClassPage .= "'''".$class->getName()."'''".' {{@todo|Beschreibung}}'.NL.NL;
			$wikiClassPage .= '<source lang="php">Der DocComment dient nur zur Referenz - bitte LÖSCHEN'.NL.$class->getDocComment().NL.'</source>'.NL.NL;

			$parent = $class->getParentClass();

			$wikiClassPage .= '==Definiert in=='.NL;
			$wikiClassPage .= '{{folder|/libraries/joomla}}'.NL;
			$wikiClassPage .= '*{{folder|'.$class->subPackage.'|red}}'.NL;
			if( $class->subSubPackage )
			{
				$wikiClassPage .= '**{{folder|'.$class->subSubPackage.'|red}}'.NL;
				$wikiClassPage .= '*';
			}
			$wikiClassPage .= '**{{file|'.$rawDoc->fileName.'}}'.NL.NL;
			$wikiClassPage .= '==Importieren=='.NL;
			$s = ($class->subSubPackage) ? $class->subSubPackage.'.' : '';
			$wikiClassPage .= '<source lang="php">jimport( \'joomla.'.$class->subPackage.'.'.$s.JFile::stripExt($rawDoc->fileName).'\' );</source>'.NL.NL;

			$wikiClassPage .= '==Erweitert=='.NL;
			$wikiClassPage .=( $parent ) ? '[['.$wikiClassPrefix.'/'.$parent->name.'|'.$parent->name.']]' : '* Keine';
			$wikiClassPage .= NL.NL;

			$wikiClassPage .= '==Erweitert von=='.NL.NL;
			//@todo
			$extendedBy = '';
			$wikiClassPage .=( $extendedBy ) ? '[['.$wikiClassPrefix.'/'.$extendedBy->name.'|'.$extendedBy->name.']]' : '* Keine';
			$wikiClassPage .= NL.NL;

			$wikiClassPage .= '==Methoden=='.NL;
			$wikiClassPage .= '{| class="wikitable"'.NL;
			$wikiClassPage .= '|-'.NL;
			$wikiClassPage .= '!Name'.NL;
			$wikiClassPage .= '!Beschreibung'.NL;

			$methods =  $class->getMethods();
			$displayClassName = '';
			foreach ($methods as $method)
			{
				$parameters = $method->getParameters();
				$wikiParams = array();
				$wikiParamsDesc = '';
				foreach( $parameters as $parameter )
				{

					if( $parameter->isDefaultValueAvailable())
					{
						$def = $parameter->getDefaultValue();

						if( $def === null)
						{
							$wikiDefault = 'null';
						}
						else if( $def === false )
						{
							$wikiDefault = 'false';
						}
						else if( $def === true )
						{
							$wikiDefault = 'true';
						}
						else if( $def === array() )
						{
							$wikiDefault = 'array()';
						}
						else if( $def === '' )
						{
							$wikiDefault = "''";
						}
						else
						{
							$wikiDefault = $def;
						}
					}
					else
					{
						$wikiDefault = 'NODEFAULT';
					}

					$wikiP = '$'.$parameter->getName();
					if( $parameter->isOptional() )
					{
						$wikiP = '['.$wikiP.']';
					}
					$wikiParams[] = $wikiP;

					$wikiParamsDesc .= '|-'.NL;
					$wikiParamsDesc .= '|<tt>'.$wikiP.'</tt>'.NL;
					$wikiParamsDesc .= '|<tt>{{@todo|Typ}}</tt>'.NL;
					$wikiParamsDesc .= '|{{@todo|Beschreibung}}'.NL;
					$wikiParamsDesc .=($wikiDefault == 'NODEFAULT') ? '| ---'.NL : '|<tt>'.$wikiDefault.'</tt>'.NL;// : '|<tt>{{@todo|Default}}</tt>'.NL;
				}//foreach parameters

				$declaringClass= $method->getDeclaringClass()->getName();
				if( strtolower($declaringClass) == strtolower($class->getName()) )
				{
					$comment = explode(NL, $method->getDocComment());
					$searches = array('@return', '@since');
					$syntaxAdds = '';
					$hasReturn = false;
					foreach ($comment as $c)
					{
						foreach ($searches as $search)
						{
							if( strpos($c, $search))
							{
								$c = str_replace($search, "'''".$search."'''", $c);
								$syntaxAdds .= trim($c).NL;
								if( $search == '@return'){ $hasReturn = true; }
							}
						}
					}//foreach

					if( ! $hasReturn ) { $syntaxAdds = "* '''@return''' {{mark|XXXX}} {{@todo}}".NL.$syntaxAdds; }

					$wikiMethodsPage = '';
					$wikiMethodsPage .= "'''".$class->getName().'/'.$method->name."'''".' {{@todo|Beschreibung}}'.NL.NL;
					$wikiMethodsPage .= '<source lang="php">Der DocComment dient nur zur Referenz - bitte entfernen'.NL.$method->getDocComment().'</source>'.NL.NL;
					$wikiMethodsPage .= '==Syntax=='.NL;
					$wikiMethodsPage .= '<source lang="php">'.$method->name.'( '.implode(', ', $wikiParams).' )</source>'.NL;
					$wikiMethodsPage .= $syntaxAdds;

					$wikiMethodsPage .= '{| class="wikitable"'.NL;
					$wikiMethodsPage .= '!Parameter'.NL;
					$wikiMethodsPage .= '!Datentyp'.NL;
					$wikiMethodsPage .= '!Beschreibung'.NL;
					$wikiMethodsPage .= '!Standardwert'.NL;

					$wikiMethodsPage .= $wikiParamsDesc;
					$wikiMethodsPage .= '|}'.NL.NL;

					$wikiMethodsPage .= '==Beispiel=='.NL;
					$wikiMethodsPage .= '{{@todo|Beispiele Baby...}}'.NL.NL;
					$wikiMethodsPage .= '<source lang="php">'.NL.'//--Dein Beispiel'.NL.'</source>'.NL.NL;

					$wikiMethodsPage .= '==Siehe auch=='.NL;
					$wikiMethodsPage .= '* <tt>[http://api.joomla.org/Joomla-Framework/'.$class->subPackageName.'/'.$class->getName().'.html#'.$method->name.' '.$class->getName().'->'.$method->name.'()]</tt> auf api.joomla.org'.NL.NL;
					$wikiMethodsPage .= '[[Kategorie:Experten|'.$method->name.']]'.NL;
					$wikiMethodsPage .= '[[Kategorie:Joomla! Programmierung|'.$method->name.']]'.NL;
					$wikiMethodsPage .= '[[Kategorie:Framework|'.$method->name.']]'.NL;
					$wikiMethodsPage .= '[[Kategorie:'.$class->getName().'|'.$method->name.']]'.NL;

					//--Everythig starting with a '_' will be ignored - aka private..
					//--@todo change in 1.6
					if( $method->name == '_' || substr($method->name, 0, 1) != '_')
					{

						$wikiMethodsPages[$method->name] = $wikiMethodsPage;
						$wikiClassPage .= '|-'.NL;
						$wikiClassPage .= '| [['.$wikiClassPrefix.'/'.$class->getName().'/'.$method->name.'|'.$method->name.']]'.NL;
						$wikiClassPage .= '|{{@todo|beschreibung}}'.NL;
					}
				}
			}//foreach methods

			$wikiClassPage .= '|}'.NL.NL;
			$wikiClassPage .= '==Siehe auch=='.NL;
			$wikiClassPage .= '* <tt>[http://api.joomla.org/Joomla-Framework/'.$class->subPackageName.'/'.$class->getName().'.html '.$class->getName().']</tt> auf api.joomla.org'.NL.NL;
			$wikiClassPage .= '[[Kategorie:Experten|'.$class->getName().']]'.NL;
			$wikiClassPage .= '[[Kategorie:Joomla! Programmierung|'.$class->getName().']]'.NL;
			$wikiClassPage .= '[[Kategorie:Framework|'.$class->getName().']]'.NL;
			$wikiClassPage .= '[[Kategorie:'.$class->getName().'|'.$class->getName().']]'.NL;

			$html .=  $pane->startPanel('Class '.$class->getName(), $class->getName().'-page');
			#			$html .= '<h3>Class '.$class->getName().'</h3>';
			$html .= '<pre>'.htmlspecialchars($wikiClassPage).'</pre>';
			$html .= $pane->endPanel();
			#$html .= '<h2>Methoden</h2>';

			foreach ($wikiMethodsPages as $pName => $page)
			{
				$title = $class->getName().' -> '.$pName;
				$html .= $pane->startPanel($title, $title.'-page');
				#				$html .= '<h3>'.$class->getName().' -> '.$pName.'</h3>';
				$html .= '<pre>'.htmlspecialchars($page).'</pre>';
				$html .= $pane->endPanel();
			}//foreach
		}//foreach classes

		$html .= $pane->endPane();

		return '<h1 style="color: orange;">Format for docs.joomla.org NOT finished yet....</h1>'.$html;
	}//function

}//class
